package com.goldendawn.lockscreen.tools;

import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.app.KeyguardManager.OnKeyguardExitResult;
import android.content.Context;

public class ManagerKeyground {
	private static KeyguardManager myKM = null;  
	private static KeyguardLock myKL = null;  
	  
	public static final String TAG = "kg";  
	  
	public static synchronized void initialize(Context context) {  
	  if (myKM == null) {  
	    myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);  
	  }  
	}  
	  
	public static synchronized void disableKeyguard(Context context) {  
	  initialize(context);  
	  
	  if (myKM.inKeyguardRestrictedInputMode()) {  
	   if(myKL==null) myKL = myKM.newKeyguardLock(TAG);  
	    myKL.disableKeyguard();  
	  } else {  
	    myKL = null;  
	  }  
	}  
	  
	//the following checks if the keyguard is even on...  
	//it actually can't distinguish between password mode or not  
	//I've learned that this returns true even if you've done the above disable  
	//that's because disable is just hiding or pausing the lockscreen.  
	  
	//the only time this returns false is if we haven't ever done an init  
	//or we have already securely exited  
	//the OS also seems to automatically do a secure exit when you press home after a disable  
	public static synchronized boolean inKeyguardRestrictedInputMode() {  
	  if (myKM != null) {  
	    return myKM.inKeyguardRestrictedInputMode();  
	  }  
	    
	  return false;  
	}  
	  
	public static synchronized void reenableKeyguard() {  
	  if (myKM != null) {  
	    if (myKL != null) {  
	      myKL.reenableKeyguard();  
	      myKL = null;  
	    }  
	  }  
	}  
	  
	//this only can be used after we have paused/hidden the lockscreen with a disablekeyguard call  
	//otherwise the OS logs "verifyunlock called when not externally disabled."  
	public static synchronized void exitKeyguardSecurely(final LaunchOnKeyguardExit callback) {  
	  if (inKeyguardRestrictedInputMode()) {  
	    myKM.exitKeyguardSecurely(new OnKeyguardExitResult() {  
	      @Override
		public void onKeyguardExitResult(boolean success) {  
	        reenableKeyguard();  
	        //this call ensures the keyguard comes back at screen off  
	        //without this call, all future disable calls will be blocked  
	        //for not following the lockscreen rules  
	        //in other words reenable immediately restores a paused lockscreen  
	        //but only queues restore for next screen off if a secure exit has been done already  
	        if (success) {  
	          callback.LaunchOnKeyguardExitSuccess();  
	        } else {  
	        }  
	      }  
	    });  
	  } else {  
	    callback.LaunchOnKeyguardExitSuccess();  
	  }  
	}  
	  
	public interface LaunchOnKeyguardExit {  
	  public void LaunchOnKeyguardExitSuccess();  
	}  
}
